# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     RedisClient
   Description :   获取到代理ip
   Author :        William
   date：          2020.2.19
-------------------------------------------------
   Change Activity:
                   2020.2.19:  获取到代理ip
-------------------------------------------------
"""
__author__ = 'William'

import time
from Config.Setting import *
from Database.RedisClient import Redis_cliient
from Encryption.python_aes import aes
import requests,traceback,json
from io import StringIO
from datetime import datetime

class Get_ip_proxy_:
    def __init__(self):
        self.proxy_json = {
           "proxy_ip":  "",
           "type":1,
           "user":"",
           "password":"",
           "last_status":   1,
           "last_time":  datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        }

    def zhandaye(self):
   
        # 最快请求频率不得超过10s一次
        # 以下是购买站大爷提供的信息
        url = ""
        api_id = ""
        api_psw = aes.encrypt("").decode('utf-8')
        try:
            response = requests.get(url,timeout=3)
            data = json.loads(response.text)
            logger.info(data)
            if data["msg"] == "获取成功":
                ip_list = data["data"]["proxy_list"]
                for x  in ip_list:
                    ip = x["ip"]
                    port = x["port"]
                    self.proxy_json.update({"proxy_ip":ip+":"+str(port),"user":api_id,"password":api_psw})
                    Redis_cliient.put(self.proxy_json)
        except:
            fp = StringIO()
            traceback.print_exc(file=fp)
            message = fp.getvalue()
            logger.error(message)

get_ip_proxy_ = Get_ip_proxy_()

if __name__ == '__main__':
    get_ip_proxy_.zhandaye()



